import { axios } from '@/utils/request'
import qs from 'qs'
import fileDownload from "js-file-download";
import Vue from "vue";

export function downFilePost(url,parameter,method = 'post'){
  return axios({
    url: url,
    data: parameter,
    method,
    responseType: 'blob',

  })
}

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url,parameter,method = 'post'){
    return axios({
      url: url,
      data: qs.stringify(parameter),
      method,
      responseType: 'blob',
      headers: { 'content-type': 'application/x-www-form-urlencoded' },
    })
  }


export function exportDetailList( parameter,method ) {
    return downFile('/vatoutinv/vmsClassifyCode/exportDetailList', parameter,method).then((data) => {
      if (!data || data.size === 0) {
        Vue.prototype['$message'].warning('文件下载失败')
        return
      }
  
      var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
        var contentDisposition = decodeURI(data.headers['content-disposition'])
        var result = patt.exec(contentDisposition)
  
  
        try {
          var fileName = result[1]
          fileName = fileName.replace(/\"/g, '')
        } catch (error) {
          return Vue.prototype['$message'].warning('文件下载失败')
        }
        if(!fileName) return Vue.prototype['$message'].warning('文件下载失败')
  
      fileDownload(data.data,fileName)
    })
  }


export function exportVmsAvalibleTaxRateLogDetail( parameter,method ) {
    return downFile('/vatoutinv/vmsAvalibleTaxRate/exportVmsAvalibleTaxRateLogDetail', parameter,method).then((data) => {
      if (!data || data.size === 0) {
        Vue.prototype['$message'].warning('文件下载失败')
        return
      }
  
      var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
        var contentDisposition = decodeURI(data.headers['content-disposition'])
        var result = patt.exec(contentDisposition)
  
  
        try {
          var fileName = result[1]
          fileName = fileName.replace(/\"/g, '')
        } catch (error) {
          return Vue.prototype['$message'].warning('文件下载失败')
        }
        if(!fileName) return Vue.prototype['$message'].warning('文件下载失败')
  
      fileDownload(data.data,fileName)
    })
  }

  export function exportVmsAvalibleTaxRate( parameter,method ) {
    return downFile('/vatoutinv/vmsAvalibleTaxRate/exportVmsAvalibleTaxRate', parameter,method).then((data) => {
      if (!data || data.size === 0) {
        Vue.prototype['$message'].warning('文件下载失败')
        return
      }
  
      var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
        var contentDisposition = decodeURI(data.headers['content-disposition'])
        var result = patt.exec(contentDisposition)
  
  
        try {
          var fileName = result[1]
          fileName = fileName.replace(/\"/g, '')
        } catch (error) {
          return Vue.prototype['$message'].warning('文件下载失败')
        }
        if(!fileName) return Vue.prototype['$message'].warning('文件下载失败')
  
      fileDownload(data.data,fileName)
    })
  }
  export function downloadExcel( parameter,method ) {
    return downFile('/vatoutinv/vmsManualInvoicing/downloadExcel', parameter,method).then((data) => {
      if (!data || data.size === 0) {
        Vue.prototype['$message'].warning('文件下载失败')
        return
      }
  
      var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
        var contentDisposition = decodeURI(data.headers['content-disposition'])
        var result = patt.exec(contentDisposition)
  
  
        try {
          var fileName = result[1]
          fileName = fileName.replace(/\"/g, '')
        } catch (error) {
          return Vue.prototype['$message'].warning('文件下载失败')
        }
        if(!fileName) return Vue.prototype['$message'].warning('文件下载失败')
  
      fileDownload(data.data,fileName)
    })
  }



  export function vmsFullOutInvoiceFileDownload( parameter,method ) {
    return downFilePost('/vatoutinv/vmsFullOutInvoice/fileDownload', parameter,method).then((data) => {
      if (!data || data.size === 0) {
        Vue.prototype['$message'].warning('文件下载失败')
        return
      }
  
      var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
        var contentDisposition = decodeURI(data.headers['content-disposition'])
        var result = patt.exec(contentDisposition)
        try {
          var fileName = result[1]
          fileName = fileName.replace(/\"/g, '')
        } catch (error) {
        }
  
        console.log(data);
        if(!fileName) return Vue.prototype['$message'].warning('文件下载失败')
      fileDownload(data.data,fileName)
    })
  }
  export function exportCode( parameter,method ) {
    return downFilePost('/vatoutinv/vmsClassifyCode/exportCode', parameter,method).then((data) => {
      if (!data || data.size === 0) {
        Vue.prototype['$message'].warning('文件下载失败')
        return
      }
  
      var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
        var contentDisposition = decodeURI(data.headers['content-disposition'])
        var result = patt.exec(contentDisposition)
        try {
          var fileName = result[1]
          fileName = fileName.replace(/\"/g, '')
        } catch (error) {
        }
  
        console.log(data);
        if(!fileName) return Vue.prototype['$message'].warning('文件下载失败')
      fileDownload(data.data,fileName)
    })
  }